/**
 * @Title: MyAsyncTask.java
 * @Package com.sinoglobal.wallet
 * @Description: TODO(用一句话描述该文件做什么)
 * @author hpq
 * @date 2015-9-8 下午2:24:50
 */
	
package com.sinoglobal.wallet.task;

import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import org.apache.http.conn.ConnectTimeoutException;

import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.dialog.LoadingDialog1;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.widget.Toast;

/**
 * @author hpq
 * @createdate 2015-9-8 下午2:24:50
 * @description: 带进度框的异步任务 取消操作或执行中遇到异常后会调用 exception();
 */
public abstract class MyAsyncTask<Result> extends AsyncTask<Void, Void, Result> 
	implements ITask<Void, Void, Result> {
	public static int RESULT_DATA = 1;
	private final int ERROR = 4;
	private final int NODATA = 5; // 没有数据
	private final int TIME_OUT = 6; // 超时
	
	private boolean cancelable = true; // isLoadingPrompt 点击返回按钮加载条是否消失
	private Context context;
	private LoadingDialog1 progressDialog;
	private String message = "加载中...";//提示信息
	private boolean isShowProgressDialog = true;//是否显示加载条

	public MyAsyncTask(Context context) {
		this.context = context;
	}

	public MyAsyncTask(boolean isShowProgressDialog, Context context) {
		this.context = context;
		this.isShowProgressDialog = isShowProgressDialog;
	}
	
	public MyAsyncTask(boolean isShowProgressDialog, Context context,boolean cancelable) {
		this.context = context;
		this.isShowProgressDialog = isShowProgressDialog;
		this.cancelable = cancelable;
	}

	public MyAsyncTask(Context context, LoadingDialog1 progressDialog) {
		this.context = context;
		this.progressDialog = progressDialog;
	}

	public MyAsyncTask(Context context, String msg) {
		this.context = context;
		this.message = msg;
	}

	public MyAsyncTask(Context context, boolean cancelable) {
		this.context = context;
		this.cancelable = cancelable;
	}

	public MyAsyncTask(Context context, boolean cancelable, boolean isShowProgressDialog) {
		this.context = context;
		this.cancelable = cancelable;
		this.isShowProgressDialog = isShowProgressDialog;
	}

	public MyAsyncTask(Context context, boolean cancelable, String msg) {
		this.context = context;
		this.cancelable = cancelable;
		this.message = msg;
	}

	public MyAsyncTask(Context context, String msg, boolean isShowProgressDialog) {
		this.context = context;
		this.isShowProgressDialog = isShowProgressDialog;
		this.message = msg;
	}

	@Override
	protected void onPreExecute() {
		if (isShowProgressDialog) {
			showDialog();
		}
		super.onPreExecute();
	}
	
	private void showDialog() {
		if (null == progressDialog) {
			progressDialog = new LoadingDialog1(context, message);
			progressDialog.setCancelable(cancelable);
//			progressDialog.setCanceledOnTouchOutside(false);
		}
		progressDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				
			}
		});
		try {
			if(progressDialog != null&&!progressDialog.isShowing() && progressDialog.getWindow() != null)
			progressDialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected Result doInBackground(Void... params) {
		try {
			return before(params);			
		} catch(UnknownHostException e){
			e.printStackTrace();
			showUiThreadInfo(TIME_OUT, this);
			cancel(false);
		}catch(SocketException e){
			e.printStackTrace();
			showUiThreadInfo(TIME_OUT, this);
			cancel(false);
		}catch(SocketTimeoutException e){
			e.printStackTrace();
			showUiThreadInfo(TIME_OUT, this);
			cancel(false);
		}catch(ConnectTimeoutException e){
			e.printStackTrace();
			showUiThreadInfo(TIME_OUT, this);
			cancel(false);
		}catch(Exception e) {
			e.printStackTrace();
			showUiThreadInfo(ERROR, this);
			cancel(false);
		}
		return null;
	}

	@SuppressWarnings("rawtypes")
	private void showUiThreadInfo(final int index, final ITask task) {
		((Activity)context).runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				switch(index){
				case ERROR:
					showShortToastMessage(context.getString(R.string.error));
					break;
				case TIME_OUT:
					showShortToastMessage(context.getString(R.string.no_netdata));
					break;
				case NODATA:
					showShortToastMessage(context.getString(R.string.no_netdata));
					break;
				default:
					break;
				}
				task.exception();
			}
		});		
		dismissDialog();
	}

	public void showShortToastMessage(String tip) {
		Toast.makeText(context, tip, Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onPostExecute(Result result) {
		super.onPostExecute(result);
		dismissDialog();
		if (result == null) {
			showUiThreadInfo(NODATA, this);
			cancel(false);
			return;
		}
		after(result);
	}

	public void dismissDialog() {
		if (progressDialog != null && progressDialog.isShowing() && progressDialog.getWindow() != null) {
			try {
				progressDialog.dismiss();
			} catch (Exception e) {
				// e.printStackTrace();
			}
		}
	}

}
